package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.DtsRegionMapper;
import com.qf.pojo.*;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DstShopServiceImpl implements DstShopService{
    @Autowired
    private DtsRegionMapper dtsRegionMapper;
    @Autowired
    private DtsBrandMapper dtsBrandMapper;
    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;
    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;




    //区域总数
    @Override
    public int regionCount() {
        return  dtsRegionMapper.selectCount(null);
    }
    //区域列表
    @Override
    public List<DtsRegion> findRegion(int page, int limit, String sort, String order) {
        Page<DtsRegion> dtsRegionPage = dtsRegionMapper.selectPage(new Page<DtsRegion>(page, limit), new QueryWrapper<DtsRegion>());
        List<DtsRegion> dtsRegionList = dtsRegionPage.getRecords();
        return dtsRegionList;
    }


    //品牌列表
    @Override
    public IPage<DtsBrand> findBrand(int page, int limit, String sort, String order, Integer id, String name) {
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name),"name",name);
        queryWrapper.eq(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort);

        return dtsBrandMapper.selectPage(new Page<DtsBrand>(page,limit),queryWrapper);
    }
    //商品种类---暂时没用
    @Override
    public List<DtsCategory> findDtsCategory() {
        List<DtsCategory> dtsCategoryList = dtsCategoryMapper.selectList(new QueryWrapper<DtsCategory>());
        return dtsCategoryList;
    }
    //商品种类-为实现需要两级列表工具类
    @Override
    public List<CatVo> findCatvo() {
        QueryWrapper<DtsCategory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",0);
        List<DtsCategory> dtsCategoryList = dtsCategoryMapper.selectList(queryWrapper);
        List<CatVo> list=new ArrayList<>();
        for (DtsCategory ca:
                dtsCategoryList) {
            QueryWrapper<DtsCategory> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("pid",ca.getId()).select("id value,name label");
            List<Map<String, Object>> maps = dtsCategoryMapper.selectMaps(queryWrapper1);
            CatVo catVo=new CatVo();
            catVo.setValue(ca.getId());
            catVo.setLabel(ca.getName());
            catVo.setChildren(maps);
            list.add(catVo);
        }
        return list;
                

    }



    //商品总数
    @Override
    public int goodsCount() {
        return  dtsGoodsMapper.selectCount(null);
    }
    //商品列表
    @Override
    public List<DtsGoods> findGoods(int page, int limit, String sort, String order) {
        Page<DtsGoods> dtsGoodsPage = dtsGoodsMapper.selectPage(new Page<DtsGoods>(page, limit), new QueryWrapper<DtsGoods>());
        List<DtsGoods> dtsGoodsList = dtsGoodsPage.getRecords();
        return dtsGoodsList;
    }

    @Override
    public void createbrand(DtsBrand dtsBrand) {
        dtsBrand.setUpdateTime(new Date());
        dtsBrand.setAddTime(new Date());
        dtsBrandMapper.insert(dtsBrand);
    }

    @Override
    public void deletebrand(DtsBrand dtsBrand) {
        QueryWrapper<DtsBrand> queryWrapper= new QueryWrapper<>();

        dtsBrandMapper.deleteById(dtsBrand);

    }
}
